package Algorithm.Tree;

/**
 * @Author cj
 * @Date 2022/1/20 21:02
 */
public class printTree {

    static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int val) {
            this.value = val;
        }
    }

    public static void printTree1(Node head) {
        System.out.println("print tree  start:");
        printOrder(head, 0, "H", 17);
        System.out.println();
    }

    private static void printOrder(Node node, int level, String prefix, int len) {
        if (node == null) {
            return;
        }
        // 先遍历右子树，自己，再遍历左子树
        printOrder(node.right, level + 1, "v", len);
        String val = prefix + node.value + prefix;
        int lenM = val.length();
        int lenL = (len - lenM) / 2;
        int lenR = len - lenL - lenM;
        val = getSpace(lenL) + val + getSpace(lenR);
        System.out.println(getSpace(level * len) + val);
        printOrder(node.left, level + 1, "^", len);
    }

    public static String getSpace(int len) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append(" ");
        }
        return sb.toString();
    }

}
